package controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Client;
import model.Organization;
import model.User;
import model.Users;

/**
 * Servlet implementation class Registration
 * This servlet handles registering a new user.
 */
public class Registration extends HttpServlet {

	private static final long serialVersionUID = 1L;
	public void init() throws ServletException {
		// Do required initialization
		//InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/config.properties"); 
	}   
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Registration() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * The doGet method is not currently used.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * The doPost method gets the username and password from the correct
	 * parameters. A Users object is created and used to attempt to add the new
	 * user to the site.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// first, set the "content type" header of the response
		response.setContentType("text/html");
		//Get username/password from the correct parameters
		String username=request.getParameter("username");
		String password=request.getParameter("pwd1");
		String organizationName=request.getParameter("organization");
		String address=request.getParameter("address");
		String fullname = request.getParameter("firstName") + " " + request.getParameter("lastName");
		
		Organization organization = new Organization();
		if(!organizationName.equals("") && organizationName != null){
			organization.setName(organizationName);
		}
		
		if(!address.equals("") && address != null){
			organization.setAddress(address);
		}
		
		//Create a Users object
		Users users = new Users();
		String hashed = users.hashPassword(password);
		User user = new User(fullname, username, hashed);

		Client client = new Client(user, organization);
		
		boolean added = false;
		try {
			added = users.addUserDB(client);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//If the user was successfully added then redirect, otherwise display
		//the appropriate error message
		if(added){
			response.sendRedirect("Login.jsp");
		} else {
			//Encode URL
			String url = "/Registration.jsp";
			request.setAttribute("loginError","This username is already taken, please choose another one");
			url = response.encodeURL(url);
			RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
			rd.forward(request, response);
		}
	}

}
